﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SportStore.Models.Entities;
using SportStore.Binders;

namespace SportStore
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "superdefault", // Route name
                "", // URL with parameters
                new { controller = "Default", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "default", // Route name
                "", // URL with parameters
                new { controller = "Products", action = "List", page = 1, category = (string)null } // Parameter defaults
            );

            routes.MapRoute(
                "pagesdefault", // Route name
                "Pages/{page}", // URL with parameters
                new { controller = "Products", action = "List", page = 1, category = (string)null } // Parameter defaults
            );

            routes.MapRoute(
                "uni1catpage", // Route name
                "{category}/", // URL with parameters
                new { controller = "Products", action = "List", page = 1 }, // Parameter defaults
                new { category = @"\w+" }
            );

            routes.MapRoute(
                "uni3pages", // Route name
                "{category}/Pages/{page}", // URL with parameters
                new { controller = "Products", action = "List" }, // Parameter defaults
                new { category = @"\w+", page = @"\d+" }
            );


            routes.MapRoute(
                "admedit", // Route name
                "Admin/Edit/{id}",
                new { controller = "Admin", action = "Edit" },
                new { id = @"\d+" }
            );

            routes.MapRoute(
                "uni2", // Route name
                "{controller}/{action}" // URL with parameters
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder());
        }
    }
}